package com.huluobo.lc.ppjoke.ui.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.huluobo.lc.libcommon.PixUtils;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * @author Lc
 * @description:
 * @date :2020/3/30 11:32
 */
public class PPImageView extends AppCompatImageView {
    public PPImageView(Context context) {
        super(context);
    }

    public PPImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PPImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @BindingAdapter(value = {"image_url", "isCircle"}, requireAll = false)
    public static void setImageUrl(PPImageView view, String imageUrl, boolean isCircle) {
        RequestBuilder<Drawable> builder = Glide.with(view).load(imageUrl);
        if (isCircle) {
            builder.transform(new CircleCrop());
        }
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (view.getLayoutParams() != null && layoutParams.width > 0 && layoutParams.height > 0) {
            builder.override(layoutParams.width, layoutParams.height);
        }
        builder.into(view);
    }

    public void bindData(int widthPx, int heightPx, int marginLeft, String url) {
        bindData(widthPx, heightPx, marginLeft, PixUtils.getScreenWidth(),
                PixUtils.getScreenHeight(), url);
    }

    public void bindData(int widthPx, int heightPx, int marginLeft, int maxWidth, int maxHeight,
                         String url) {
        if (widthPx <= 0 || heightPx <= 0) {
            Glide.with(this).load(url).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<?
                        super Drawable> transition) {
                    int intrinsicHeight = resource.getIntrinsicHeight();
                    int intrinsicWidth = resource.getIntrinsicWidth();
                    setSize(intrinsicWidth, intrinsicHeight, marginLeft, maxWidth, maxHeight);

                    setImageDrawable(resource);
                }
            });
            return;
        }

        setSize(widthPx, heightPx, marginLeft, maxWidth, maxHeight);
        setImageUrl(this, url, false);
    }

    private void setSize(int intrinsicWidth, int intrinsicHeight, int marginLeft, int maxWidth,
                         int maxHeight) {
        int finalWidth, finalHeight;
        if (intrinsicWidth > intrinsicHeight) {
            finalWidth = maxWidth;
            finalHeight = (int) (intrinsicHeight / (intrinsicWidth * 1.0f / finalWidth));
        } else {
            finalHeight = maxHeight;
            finalWidth = (int) (intrinsicWidth / (intrinsicHeight * 1.0f / finalHeight));
        }


        ViewGroup.LayoutParams params = getLayoutParams();
        params.width = finalWidth;
        params.height = finalHeight;
        if (params instanceof FrameLayout.LayoutParams) {
            ((FrameLayout.LayoutParams) params).leftMargin = intrinsicHeight > intrinsicWidth ?
                    PixUtils.dp2px(marginLeft) : 0;
        } else if (params instanceof LinearLayout.LayoutParams) {
            ((LinearLayout.LayoutParams) params).leftMargin = intrinsicHeight > intrinsicWidth ?
                    PixUtils.dp2px(marginLeft) : 0;
        }
        setLayoutParams(params);
    }

    public void setBlurImageUrl(String coverUrl, int radius) {
        Glide.with(this).load(coverUrl).override(50).transform(new BlurTransformation()).dontAnimate().into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<?
                    super Drawable> transition) {
                setBackground(resource);
            }
        });
    }
}
